package shuzi.activity;

import shuzi.data.Database;
import shuzi.util.Dialog;
import shuzi.util.MySharedPreferences;
import shuzi.util.Webserice;
import shuzi.util.WifiControl;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.shuzi.offer.R;

public class RegistetActivity extends Activity {

	private Handler handler;
	private final int HANDLER_REGISTER_SUCCESS = 10;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.phone_register);
		initHandler();
		initLayout();

	}

	private void initLayout() {
		 final EditText phoneNumber = (EditText) findViewById(R.id.phone_number);
		 final EditText dealerContactNumber = (EditText) findViewById(R.id.deliver_number);
		 final EditText deliveryAddress = (EditText) findViewById(R.id.deliver_address);
		 final EditText deliveryName = (EditText) findViewById(R.id.deliver_name);
		 final Button registet = (Button) findViewById(R.id.register);
		 
		 registet.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				new Thread(new Runnable() {

					@Override
					public void run() {
						final String data = Webserice.saveTermInfo(WifiControl.getLocalMacAddress(RegistetActivity.this),
								phoneNumber.getText().toString(),
								dealerContactNumber.getText().toString(),
								deliveryAddress.getText().toString());
						RegistetActivity.this.runOnUiThread(new Runnable() {

							@Override
							public void run() {
								System.out.println(data
										+ Webserice.isRegisterSuccess(data));
								// Toast.makeText(context,
								// data+Webserice.isRegisterSuccess(data),
								// Toast.LENGTH_LONG).show();
								if (Webserice.isRegisterSuccess(data)) {
									
									
									MySharedPreferences
											.writeStringSharedPreferences(
													RegistetActivity.this,
													Database.LOGIN_FILE_NAME,
													Database.PHONE_NUMBER, phoneNumber.getText().toString());
									MySharedPreferences
									.writeStringSharedPreferences(
											RegistetActivity.this,
											Database.LOGIN_FILE_NAME,
											Database.DELIVER_NUMBER, dealerContactNumber.getText().toString());
									MySharedPreferences
									.writeStringSharedPreferences(
											RegistetActivity.this,
											Database.LOGIN_FILE_NAME,
											Database.DELIVER_ADDRESS, deliveryAddress.getText().toString());
									handler.sendEmptyMessage(HANDLER_REGISTER_SUCCESS);
								} else {
									handler.sendEmptyMessage(Database.HANDLER_REFRESH_ERROR);
								}
							}
						});

					}
				}).start();

				
			}
		});

	}

	/**
	 * 初始化handler
	 */
	private void initHandler() {
		handler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);
				switch (msg.what) {
				case HANDLER_REGISTER_SUCCESS:
					Toast.makeText(RegistetActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
					Intent in = new Intent();
					in.setClass(RegistetActivity.this, FirstPageActivity.class);
					startActivity(in);
					finish();

					break;
					
				case Database.HANDLER_REFRESH_ERROR:
					Dialog.registetError(RegistetActivity.this);
					break;

				default:
					break;
				}
			}
		};

	}

}
